﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReAnimation.MVP.ViewInterfaces;
using GLWrap;
using Sharp3D.Math.Core;

namespace ReAnimation.MVP.Views
{
    public partial class CameraPositionSettingsView : BaseUserControlView,ICameraPositionSettingsView
    {
        private bool _internalChange;
        ICamera _cam;
        IGlSceneManager _glSceneManager;
        public CameraPositionSettingsView(IGlSceneManager glSceneManager)
        {
            _glSceneManager = glSceneManager;
            InitializeComponent();
            _cam = _glSceneManager.Camera;
            _glSceneManager.Camera.CameraPositionChanged += 
               Camera_CameraPositionChanged;
            
            _glSceneManager.CameraChanged += (s, o) =>
            {
                _cam.CameraPositionChanged -= Camera_CameraPositionChanged;
                _cam = _glSceneManager.Camera;
                _cam.CameraPositionChanged += Camera_CameraPositionChanged;
                Camera_CameraPositionChanged(null, EventArgs.Empty);
            };
            Camera_CameraPositionChanged(null, EventArgs.Empty);
        }

        void Camera_CameraPositionChanged(object sender, EventArgs e)
        {
            if(_internalChange) return;
            eyeXTextField.Value = _cam.Eye.X;
            eyeYTextField.Value = _cam.Eye.Y;
            eyeZTextField.Value = _cam.Eye.Z;

            viewXTextField.Value = _cam.View.X;
            viewYTextField.Value = _cam.View.Y;
            viewZTextField.Value = _cam.View.Z;
        }

        private void defaultBtn_Click(object sender, EventArgs e)
        {
            eyeXTextField.Value = 0;
            eyeYTextField.Value = 0;
            eyeZTextField.Value = 0;

            viewXTextField.Value = 0;
            viewYTextField.Value = 0;
            viewZTextField.Value = -1;
        }

        private void applyBtn_Click(object sender, EventArgs e)
        {
            _internalChange = true;
            _cam.Eye = new Vector3F(
                 eyeXTextField.Value, eyeYTextField.Value, eyeZTextField.Value
                );
            _cam.View = new Vector3F(
                viewXTextField.Value, viewYTextField.Value, viewZTextField.Value
                );
            _internalChange = false;
            _glSceneManager.Refresh();
        }
    }
}
